//
//  DTNavigationController.m
//  DayToDo
//
//  Created by karry on 2021/1/18.
//

#import "DTNavigationController.h"


@interface DTNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation DTNavigationController
//- (BOOL)prefersStatusBarHidden{
//    return YES;
//}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

 }

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    UIImage *back = [UIImage imageNamed:@"ic_back"];
    self.navigationBar.backIndicatorImage = back;
    self.navigationBar.backIndicatorTransitionMaskImage = back;
    [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16]}];

    [self.navigationBar setShadowImage:[UIImage new]];
    self.navigationBar.tintColor = [UIColor blackColor];

    __weak typeof(self) weakself = self;
     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
         self.interactivePopGestureRecognizer.delegate = (id)weakself;
     }
    
    //字体改变的通知
    @weakify(self)
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:@"SYSTEMFONTCHANGENOTIFICATIONNAME" object:nil]subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self)

        [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16]}];
    }];
    
    self.modalPresentationStyle = 0;
    
}


#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        // 屏蔽调用rootViewController的滑动返回手势
        if (self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
            return NO;
        }
    }
    return YES;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return self.viewControllers.count != 1 && [gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return self.viewControllers.count != 1 && [gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

//拦截push
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.childViewControllers.count>0) {
        viewController.hidesBottomBarWhenPushed = YES;
        UIViewController *c_vc = self.viewControllers.lastObject;
        if (c_vc) {
            c_vc.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
        }
        if([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = YES;
            self.interactivePopGestureRecognizer.delegate = self;
        }
    }
    
    [super pushViewController:viewController animated:animated];

}



- (void)goBack{
    [self popViewControllerAnimated:YES];
}


@end
